import java.util.Arrays;

public class MyPriorityqueue {
    int[] elem = new int[10] ;
    int size = 0;
    public void offer(int val) throws Exception {
        if(size== elem.length){
            throw new Exception("容量已满");
        }
        elem[size] = val;
        shiftUp(elem,size);
        size++;
    }

    public int poll(){
        int temp = elem[0];
        elem[0] = elem[size-1];
        elem[size-1] = temp;
        shiftdown(elem,0);
        size--;
        return temp;
    }



    public int peek(){
        if(size==0){
            return 0;
        }
        return elem[0];
    }
    public  void createHeap(int[] arr){
        int root = (arr.length-2)>>1;
        while(root>=0){
            shiftdown(arr,root);
            root--;
        }

    }
    public  void shiftdown(int[] arr,int parent){
        int child = parent*2+1;
        int size = arr.length;
        while(child<size){
            if(child+1<size&&arr[child+1]<arr[child]) {
                    child += 1;
            }
            if(arr[parent]<=arr[child]){
                break;
            }
            else {
                int temp = arr[parent];
                arr[parent] = arr[child];
                arr[child] = temp;
            }
            parent =child;
            child = parent*2+1;
        }
    }
    public void shiftUp(int[]arr ,int child){
        int parent = (child-1)>>1;
        while(parent>=0){
            if(arr[parent]<=arr[child]){
                break;
            }
            int temp = arr[child];
            arr[child] = arr[parent];
            arr[parent] = temp;
            child = parent;
            parent = (child-1)>>1;
        }

    }
//    public  MyPriorityqueue(int[] arr){
//        createHeap(arr);
//        this.elem = arr;
//        this.size = arr.length;
//    }


    public static void main(String[] args) throws Exception {
         MyPriorityqueue myPriorityqueue = new MyPriorityqueue();
         myPriorityqueue.offer(5);
        myPriorityqueue.offer(4);
        myPriorityqueue.offer(3);
        myPriorityqueue.offer(2);
        System.out.println(myPriorityqueue.peek());
        System.out.println(Arrays.toString(myPriorityqueue.elem));
    }
}
